package com.googlecode.c4e.actions;

import org.apache.maven.continuum.xmlrpc.client.ContinuumXmlRpcClient;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.actions.ActionDelegate;

import com.googlecode.c4e.ContinuumHelper;
import com.googlecode.c4e.model.Project;

public class BuildAction extends ActionDelegate {

  private Project project;

  @Override
  public void selectionChanged(IAction action, ISelection selection) {
    if (selection != null && selection instanceof IStructuredSelection) {
      IStructuredSelection isel = (IStructuredSelection) selection;
      if (isel.getFirstElement() instanceof Project) {
        this.project = (Project) isel.getFirstElement();
      }
    }
  }

  @Override
  public void run(IAction action) {
    if (project != null) {
      try {
        ContinuumXmlRpcClient client = ContinuumHelper.createClient(project.getProfileId());
        client.buildProject(project.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}
